﻿using ProjScan.RecordManager.AssetData;
using ProjScan.ScanRule;
using ProjScan.Tools;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager
{
    internal class AnimationClipRecordManager : RecordManagerBase<AnimationClipAssetData>
    {
        public override string TableTag => "animationclip";

        public override void PrepareAssets()
        {
            datas = new List<AnimationClipAssetData>();
            string[] source = new string[2]
            {
                "anim",
                "fbx"
            };
            int num = AssetPathManager.Instance.artPathList.Length;
            for (int i = 0; i < num; i++)
            {
                ToolsHelper.ShowProgressBar("Proj Scan [" + TableTag + "]", "Preparing " + TableTag + "(" + datas.Count + ")...", (float)i * 1f / (float)num);
                string text = AssetPathManager.Instance.artPathList[i];
                string value = Path.GetExtension(text).ToLower().Replace(".", "");
                if (!source.Contains(value))
                {
                    continue;
                }

                ActiveAllRecord();
                if (!CheckAssetPathRules(text))
                {
                    continue;
                }

                ModelImporter modelImporter = AssetImporter.GetAtPath(text) as ModelImporter;
                if (modelImporter != null)
                {
                    Object[] array = AssetDatabase.LoadAllAssetsAtPath(text);
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text);
                    if (array == null)
                    {
                        continue;
                    }

                    Object[] array2 = array;
                    foreach (Object @object in array2)
                    {
                        AnimationClip animationClip = @object as AnimationClip;
                        if (animationClip != null && animationClip.name != null && !animationClip.name.Contains("__preview__"))
                        {
                            AnimationClipAssetData animationClipAssetData = new AnimationClipAssetData(AssetPathManager.Instance.GetID(Path.GetDirectoryName(text)), text, fileNameWithoutExtension, animationClip, modelImporter);
                            animationClipAssetData.id = datas.Count;
                            ValidateAsset(animationClipAssetData);
                            animationClipAssetData.Reset();
                            datas.Add(animationClipAssetData);
                            if (datas.Count % 100 == 0)
                            {
                                ToolsHelper.GC();
                            }
                        }
                    }

                    continue;
                }

                AnimationClip animationClip2 = AssetDatabase.LoadAssetAtPath(text, typeof(AnimationClip)) as AnimationClip;
                if (!(animationClip2 != null))
                {
                    continue;
                }

                AnimationClipAssetData animationClipAssetData2 = new AnimationClipAssetData(AssetPathManager.Instance.GetID(Path.GetDirectoryName(text)), text, animationClip2, null);
                animationClipAssetData2.id = datas.Count;
                foreach (RecordRule recordRule in recordRules)
                {
                    if (dicEnableRules[recordRule])
                    {
                        recordRule.Check(animationClipAssetData2);
                    }
                }

                animationClipAssetData2.Reset();
                datas.Add(animationClipAssetData2);
                if (datas.Count % 100 == 0)
                {
                    ToolsHelper.GC();
                }
            }
        }
    }
}